/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uy.edu.ort.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 *
 * @author 153287-154559
 */
public class Utils {
    
     static Properties prop;

    private static Properties getProperties() throws FileNotFoundException, IOException {
        prop = new Properties();
        prop.load(new FileInputStream("c:/agencia noticias/settings.properties"));
        
        return prop ;
    }
    
    public static String getPropertiesValue(String key) {
        String ret = null;
        try {
            return getProperties().getProperty(key);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return ret;
    }
    
    public static String EncriptarDatos(String datos) {

        String keyString = "averylongtext";
        String input = datos;
        String ret = null;
        try {     
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            byte[] iv = new byte[cipher.getBlockSize()];

            IvParameterSpec ivSpec = new IvParameterSpec(iv);

            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            digest.update(keyString.getBytes());

            byte[] key = new byte[16];
            System.arraycopy(digest.digest(), 0, key, 0, key.length);
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");


            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
            byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8"));
            String decoded = new String(encrypted, "ISO-8859-1");

            ret = decoded;
        } catch (Exception ex) {
        }
        return ret;
    }

    public static String DesencriptarDatos(String datos) {
        byte[] encrypted;
        String ret;
        try {
            encrypted = datos.getBytes("ISO-8859-1"); 
    
            String keyString = "averylongtext";

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
               byte[] iv = new byte[cipher.getBlockSize()];
       
            IvParameterSpec ivSpec = new IvParameterSpec(iv);

            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            digest.update(keyString.getBytes());
            byte[] key = new byte[16];
            System.arraycopy(digest.digest(), 0, key, 0, key.length);
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");

            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            byte[] decrypted = cipher.doFinal(encrypted);
            ret = new String(decrypted, "UTF-8");
        } catch (Exception ex) {
            ret = "";
        }
        return ret;
    }
    
    
}